package com.gancp.common.message;

import com.alibaba.fastjson.JSONObject;
import com.gancp.utills.Md5Utils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by GCP on 2018/5/7.
 */
public class MiaoDiMsg {

    private String url = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
    private String accountSid = "";
    private String acountToken = "";
    private SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );

    public void sendMessage(Map params) throws IOException, URISyntaxException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost();
        request.setURI( new URI( url ) );
        request.setHeader( "Content-type", "application/x-www-form-urlencoded" );
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String value = String.valueOf( params.get( name ) );
            nvps.add( new BasicNameValuePair( name, value ) );
        }
        request.setEntity( new UrlEncodedFormEntity( nvps, HTTP.UTF_8 ) );
        HttpResponse response = httpClient.execute( request );
        int code = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString( entity, "UTF-8" );
        JSONObject json = JSONObject.parseObject( result );
        System.out.println( json.toString() );
    }

    public Map buildParams(String phoneNember) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        Map map = new HashMap();
        map.put( "accountSid", accountSid );
        map.put( "smsContent", "【疾风剑豪】登录验证码：{往事如风}，爱用不用。 --来自gcp的小尾巴" );
        map.put( "to", phoneNember );
        String date = dateFormat.format( new Date() );
        map.put( "timestamp", date );
        // String sig = accountSid.concat( acountToken ).concat( date );
        String sig = DigestUtils.md5Hex( accountSid.concat( acountToken ).concat( date ) );
        map.put( "sig", sig );
        return map;
    }

    public static void main(String[] args) throws Exception {
       /* MiaoDiMsg msg = new MiaoDiMsg();
        msg.sendMessage( msg.buildParams( "18702604727" ) );*/

        String nodepath = MiaoDiMsg.class.getClass().getClassLoader().getResource( "/" ).getPath();
        // 项目的根目录路径
        String filePath = nodepath.substring( 1, nodepath.length() - 16 );

    }

}
